
define(['config/appregister','jquery'],function(app){
    app.directive("lunbo",function($interval){
        return {
            templateUrl:"app/business/index/template/lunbo.html",
            scope:{
                data:'='
            },
            link:function(scope,element,attrs){
                scope.flag=false;
                var len=scope.data.length;
                var w=750;
                $('.imgList').css('width',(len+1)*w);

                var i= 0,j=0;
                function play(dir,len,width,m){
                    if(m!=undefined){
                        i=m;
                        j=m;
                    }
                    if(dir!=''){
                        if(dir=='next'){i++;j++}
                        if(dir=='pre'){i--;j--}
                    }
                    if(j>len-1){j=0;}
                    if(i>len){i=1;$('.imgList').css('left',0)}
                    if(i<0){i=len-1;j=len-1;$('.imgList').css('left',-width*len)}
                    $('.btnList>li').eq(j).addClass('cur').siblings().removeClass('cur');
                    $('.imgList').stop().animate({left:-width*i},600);
                }

                //左右方向
                scope.dirction=function(dir){
                    play(dir,len,w);
                };
                //自动轮播
                var timer=$interval(function(){
                    scope.dirction('next')
                },3000);
                //切换页面清除潜在定时器
                scope.$on("$destroy", function() {
                        $interval.cancel( timer );
                });

                //小圆点hover
                scope.hoverDot=function(m){
                    play('',len,w,m);
                };

                //hove
                $('.bannerCon').hover(function(){
                    scope.$apply(function(){
                        $interval.cancel(timer);
                        scope.flag=true;
                    })
                },function(){
                    scope.$apply(function(){
                        timer=$interval(function(){
                            scope.dirction('next')
                        },3000);
                        scope.flag=false;
                    })
                })
            }
        }
    });
});
